////#include <stdio.h>
////#include <assert.h>
////
////char* my_strstr(const char* haystack, const char* needle) {
////    assert(haystack != NULL && needle != NULL); 
////
////    if (*needle == '\0') {
////        return (char*)haystack;
////    }
////
////    while (*haystack != '\0') {
////        const char* h = haystack;
////        const char* n = needle;
////
////        while (*h != '\0' && *n != '\0' && *h == *n) {
////            h++;
////            n++;
////        }
////
////        if (*n == '\0') {
////            return (char*)haystack;
////        }
////
////        if (*h == '\0') {
////            return NULL;
////        }
////
////        haystack++;
////    }
////
////    return NULL; 
////}
////int main() {
////
////    const char* text = "Hello, this is a string for testing strstr function.";
////    const char* test1 = "this";
////    const char* test2 = "string";
////    const char* test3 = "strstr";
////    const char* test4 = "function.";
////    const char* test5 = "notfound";
////    const char* test6 = "";
////    const char* test7 = "Hello";
////    const char* test8 = "ing";
////    const char* test9 = "for testing strstr";
////    const char* test10 = "function.x";
////
////    printf("朴素算法实现:\n");
////    char* result1 = my_strstr(text, test1);
////    printf("查找 '%s': %s\n", test1, result1 ? result1 : "未找到");
////
////    char* result2 = my_strstr(text, test5);
////    printf("查找 '%s': %s\n", test5, result2 ? result2 : "未找到");
////
////    char* result3 = my_strstr(text, test6);
////    printf("查找空字符串: %s\n\n", result3);
////}
//
//#include <stdio.h>
//#include <assert.h>
//
//char* my_strncpy(char* dest, const char* src, size_t n) {
//    assert(dest != NULL && src != NULL); 
//
//    char* start = dest;  
//
//    while (n > 0 && *src != '\0') {
//        *dest++ = *src++;
//        n--;
//    }
//
//    while (n > 0) {
//        *dest++ = '\0';
//        n--;
//    }
//
//    return start;
//}
//int main() {
//    char dest1[20];
//    char dest2[20];
//    char dest3[20];
//    char dest4[20] = "Existing data";
//
//    const char* src1 = "Hello, World!";
//    const char* src2 = "Short";
//    const char* src3 = "";
//
//    printf("标准实现:\n");
//    printf("复制前: dest1 = [");
//    for (size_t i = 0; i < sizeof(dest1); i++) {
//        printf("%02X ", (unsigned char)dest1[i]);
//    }
//    printf("]\n");
//
//    char* result1 = my_strncpy(dest1, src1, 10);
//    printf("复制后 (n=10): \"%s\"\n", dest1);
//    printf("内存内容: [");
//    for (size_t i = 0; i < sizeof(dest1); i++) {
//        printf("%02X ", (unsigned char)dest1[i]);
//    }
//    printf("]\n\n");
//}

#include <stdio.h>
#include <assert.h>


char* my_strncat(char* dest, const char* src, size_t n) {
    assert(dest != NULL && src != NULL); 

    char* orig_dest = dest; 


    while (*dest != '\0') {
        dest++;
    }

    
    while (n > 0 && *src != '\0') {
        *dest++ = *src++;
        n--;
    }

   
    *dest = '\0';

    return orig_dest;
}
int main() {
   
    char str1[50] = "Hello, ";
    char str2[50] = "";
    char str3[50] = "Start";
    char str4[50] = "Existing";

    const char* append1 = "World!";
    const char* append2 = "This is a long string for testing";
    const char* append3 = "";

    
    printf("标准实现:\n");
    printf("追加前: \"%s\"\n", str1);
    char* result1 = my_strncat(str1, append1, 10);
    printf("追加后 (n=10): \"%s\"\n\n", result1);
}